//
//  FSWRegisterController.m
//  quhuolang_driver
//
//  Created by 曹欣欣 on 2018/3/6.
//  Copyright © 2018年 fusw. All rights reserved.
//

#import "FSWRegisterController.h"
 

#import "FSWBaseWebViewController.h"
@interface FSWRegisterController ()
@property (weak, nonatomic) IBOutlet UITextField *mobileText;
@property (weak, nonatomic) IBOutlet UIButton *sendButton;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UITextField *pwdText;
@property (weak, nonatomic) IBOutlet XCBaseButton *submitButton;
@property (weak, nonatomic) IBOutlet UIButton *selectButton;
@property (weak, nonatomic) IBOutlet UILabel *protocolLabel;

@property (weak, nonatomic) IBOutlet UITextField *invitationText;
@end

@implementation FSWRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    
    @weakify(self)
    [[self.mobileText rac_textSignal] subscribeNext:^(NSString* x) {
        @strongify(self)
        self.mobileText.text = x.length > 11 ? [x substringToIndex:11]: x;
        if (11 == self.mobileText.text.length && [FSWHelperTool isValidTelNum:x]) {
           self.sendButton.enabled = YES;
        }else{
            self.sendButton.enabled = NO;
        }
    }];
    
    [[RACSignal combineLatest:@[self.mobileText.rac_textSignal,
                self.codeText.rac_textSignal,self.pwdText.rac_textSignal,RACObserve(self, selectButton.selected)]
                       reduce:^(NSString* phone, NSString* code,NSString *pwd,NSNumber *connect){
                           return @(phone.length == 11 && code.length > 0 && pwd.length >=8 && [connect boolValue]);
                       }]
     subscribeNext:^(id x) {
        @strongify(self);
         if ([x boolValue]) {
             self.submitButton.enabled = YES;
             self.submitButton.backgroundColor = XCMainColor;
         }else{
             self.submitButton.enabled = NO;
             self.submitButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
         }
     }];
    
    self.protocolLabel.attributedText = [FSWHelperTool textStrLastColor:self.protocolLabel.text andColor:XCMainColor andFontSize:0 andLength:9];
    
    self.protocolLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *protocolLabelTap = [[UITapGestureRecognizer alloc] init];
    [[protocolLabelTap rac_gestureSignal] subscribeNext:^(id x) {
        @strongify(self)
        FSWBaseWebViewController * webviewcontroller = (FSWBaseWebViewController*)[self instantiateViewControllerWithStoryboardName:@"Web" identifier:@"FSWBaseWebViewController"];
        //        webviewcontroller.url = @"http://www.baidu.com";
     
        [self pushViewController:webviewcontroller];
    }];
    [self.protocolLabel addGestureRecognizer:protocolLabelTap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sendButton:(id)sender {
//    self.imageCodeView = (FSWImageCodeView*)[[NSBundle mainBundle] loadNibNamed:@"FSWImageCodeView" owner:nil options:nil].firstObject;
//    @weakify(self);
//    [self.imageCodeView show:1 addMessage:@"" andOkBlock:^(NSString *code) {
//        @strongify(self);
//        NSDictionary *dict =@{@"mobile":self.mobileText.text,@"captcha":code};
//         NSDictionary *dict =@{@"mobile":self.mobileText.text};
//        [LoginNetRequest sendMessageCodeRequest:dict andBlock:^(id responseObject) {
//            self.sendButton.enabled = NO;
//            [self.sendButton buttonCode:60 andBlock:^{
//                @strongify(self);
//                self.sendButton.enabled = YES;
//            }];
//        } andFailBlock:^(NSString *error) {
//            
//        }];
//    }];
 
}


- (IBAction)submitButton:(id)sender {
//    @weakify(self);
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (!self.invitationText) {
        dict[@"invitation_code"] = self.invitationText.text;
    }
    dict[@"mobile"] = self.mobileText.text;
    dict[@"captcha"] = self.codeText.text;
    dict[@"password"] = self.pwdText.text;
    
//    [LoginNetRequest registerRequest:dict andBlock:^(id responseObject) {
//        @strongify(self);
//        [fUserDefaults setObject:responseObject[@"access_token"] forKey:ACCESS_TOKEN];
//        [fUserDefaults setObject:responseObject[@"jpush_username"] forKey:JPUSH_USERNAME];
//        NSString *jpush_username = dict[@"jpush_username"];
//        if (jpush_username.length > 0) {
//            [self configJpush:jpush_username];
//        }
////        self.toastAlertView = (FSWToastAlertView*)[[NSBundle mainBundle] loadNibNamed:@"FSWToastAlertView" owner:nil options:nil].firstObject;
////        [self.toastAlertView show:1 addMessage:@"实名认证之后才可以发货，是否实名认证" andOkBlock:^{
////            @strongify(self);
////            FSWAuthController *vc = [self instantiateViewControllerWithStoryboardName:@"My" identifier:@"FSWAuthController"];
////            vc.type = 0;
////            vc.pageToType = 1;
////            [self pushViewController:vc];
////        } andCancelBlock:^{
////            @strongify(self);
////            [self pop];
////        }];
//    } andFailBlock:^(NSString *error) {
//
//    }];
}
- (IBAction)selectButton:(UIButton *)sender {
    sender.selected = !sender.selected;
}

@end
